package source.events.action;

import source.model.*;
import source.model.utility.*;
import source.model.type.*;
import source.view.MainScreen;

public class DisbandArmy implements Action{
	
	private int pid;

	public DisbandArmy(int pid){
		this.pid = pid;
	}

	public ActionResult execute( DataHandler dh ){
		GameObject go = dh.getCurrentSelection();

		Position armyPos = dh.positionQuery(go.objectID());

		if(go.objectType() != GameObjectType.UNIT){
			MainScreen.writeToConsole("Selected object is not an army");
		}

		Unit maybeArmy = (Unit)go;
		if(maybeArmy.getMissionFlag())
		{
			MainScreen.writeToConsole("Current selection is on a mission");
			return ActionResult.IMPOSSIBLE;
		}
		if(maybeArmy.getType() != UnitType.ARMY){
			MainScreen.writeToConsole("Selected unit is not an army");
			return ActionResult.FAILED;
		}

		Army army = (Army)maybeArmy;
	
		GameObject[] units = army.toGameObjectArray();

		if(dh.reassign(army, units, armyPos, pid)){
			MainScreen.writeToConsole("Army successfully disbanded");
			return ActionResult.SUCCESS;
		}
		else{
			MainScreen.writeToConsole("Army failed to disband");
			return ActionResult.FAILED;
		}
	}
}	


